package com.maoye.ossp.common.web.taglib;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.maoye.ossp.sys.att.entity.Attachment;
import com.maoye.ossp.sys.att.service.IAttService;

/**
 * 通过用户ID查询用户名称
 * 
 * @author zxpfss
 * 
 */
public class ListAttShowTag extends RequestContextAwareTag {

	Logger logger = Logger.getLogger(ListAttShowTag.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8033078121407287559L;
	private String prefixURL = "/sys/att";
	private int indexInThisPage;
	/**
	 * 处理的下载的URL
	 */
	private String handlerDownloadURL = prefixURL + "/download";
	/**
	 * 下载列表
	 */
	private String downloadFileListURL = prefixURL + "/attr/download/list";

	private String modleName;
	private String key;
	private String soureBillID;

	private IAttService attService;

	/**
	 * 
	 * @author xiaohan
	 * @data 2014年3月31日
	 */
	private void iniPath() {
		handlerDownloadURL = prefixURL + "/getFile";
		downloadFileListURL = prefixURL + "/attr/download/list";
	}

	protected int doStartTagInternal() throws Exception {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

			if (!prefixURL.startsWith(request.getContextPath())) {
				prefixURL = request.getContextPath() + prefixURL;
				iniPath();
			}
			Attachment param = new Attachment();
			param.setModleName(modleName);
			param.setKey(key);
			param.setSoureBillID(soureBillID);
			List<Attachment> attachments = getAttService().findListByBill(param);
			StringBuilder sb = new StringBuilder();
			if (attachments != null && !attachments.isEmpty()) {
				for (Attachment attachment : attachments) {
					sb.append("<p class=\"name\">" + attachment.getFileName() + "</p>");
					sb.append("&nbsp;&nbsp;");
					sb.append("<a href=\""+getHandlerDownloadURL()+"/"+attachment.getId().toString()+"\" title=\""+attachment.getFileName()+"\">");
					sb.append("<button type=\"submit\" class=\"btn btn-warning img_padding_btn img_margin btn_3\">");
					sb.append("<i class=\"glyphicon glyphicon-download-alt\"></i>");
					sb.append("<span>下载</span>");
					sb.append("</br>");
				}
			}
			pageContext.getOut().print(sb.toString());
		} catch (Exception e) {
			logger.debug(e);
		}
		return EVAL_BODY_INCLUDE;

	}

	public String getModleName() {
		return modleName;
	}

	public void setModleName(String modleName) {
		this.modleName = modleName;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getSoureBillID() {
		return soureBillID;
	}

	public void setSoureBillID(String soureBillID) {
		this.soureBillID = soureBillID;
	}

	public String getHandlerDownloadURL() {
		return handlerDownloadURL;
	}

	public void setHandlerDownloadURL(String handlerDownloadURL) {
		if (StringUtils.isBlank(handlerDownloadURL)) {
			return;
		}
		this.handlerDownloadURL = handlerDownloadURL;
	}

	public String getDownloadFileListURL() {
		return downloadFileListURL;
	}

	public void setDownloadFileListURL(String downloadFileListURL) {
		if (StringUtils.isBlank(downloadFileListURL)) {
			return;
		}
		this.downloadFileListURL = downloadFileListURL;
	}

	public int getIndexInThisPage() {
		return indexInThisPage;
	}

	public void setIndexInThisPage(int indexInThisPage) {
		this.indexInThisPage = indexInThisPage;
	}

	public IAttService getAttService() {
		if (this.attService == null) {
			this.attService = (IAttService) getRequestContext().getWebApplicationContext().getBean("attServiceImpl");
		}
		return attService;
	}

}
